//上传图片报警相关      http://blog.csdn.net/u010161082/article/details/46618947

function showEquip(rowIndex){
	$('#equip_data').datagrid('selectRow', rowIndex);  
    var row = $('#equip_data').datagrid('getSelected');  
    if (row.id != null && row.id !== undefined){
    	var editPhotoWin = $('#editPhotoWin');
    	$.post(basePath+"/equip/load.json",{'id':row.id},function(res){
    		editPhotoWin.form('load', {
				id:res.data.id
			}).dialog('open');
	  		if(res.data.equPhoto){
	  			$("#editimg").attr("src",basePath+'/up/'+res.data.equPhoto);
	  		}else{
	  			$("#editimg").attr("src",basePath+'/resources/img/p1.png');
	  		}
	  });
    }
    	
    
	}

obj = {
	addWS:function () {
		var awin = $('#addWsWin');
		$("#addisLine").val('N');
		awin.panel({title: "新增车间"}).dialog("open");
	},
	addLine:function(){
		var node = $('#line_data').treegrid('getSelected');
		if(node!=null){
			if(node.fatherId==null&&node.fatherId!=""){
				var awin = $('#addLineWin');
				$("#addfatherId").val(node.id);
				$("#addisLine").val('Y');
				awin.panel({title: "新增生产线"}).dialog("open");
			}else{
				$.messager.alert("提示信息","请选中一个车间!");
			}
		}else{
			$.messager.alert("提示信息","请选择一条记录!");
		}
	},
	edit:function(){
		  var node = $('#line_data').treegrid('getSelected');
		  if(node!=null){
			  var eWswin = $('#editWsWin');
		  	  var eLinewin = $('#editLineWin');
		  	  if(node.isLine=="否"){
		  		  eWswin.panel({title: "编辑车间"});
		  		  $.post(basePath+"/lineConfig/load.json",{'id':node.id},function(res){
		  			  eWswin.form('load', {
		  				  name : res.data.name,
		  				  leader : res.data.leader,
		  				  remark : res.data.remark,
		  				  isLine:res.data.isLine,
		  				  id:res.data.id
		  			  }).dialog('open');
		  		  });
		  	  }else{
		  		  eLinewin.panel({title: "编辑生产线"});
		  		  $.post(basePath+"/lineConfig/load.json",{'id':node.id},function(res){
		  			  eLinewin.form('load', {
		  				  name : res.data.name,
		  				  leader : res.data.leader,
		  				  remark : res.data.remark,
		  				  isLine:res.data.isLine,
		  				  linesNo:res.data.linesNo,
		  				  isCollect:res.data.isCollect,
		  				  fatherId:res.data.fatherId,
		  				  id:res.data.id
		  			  }).dialog('open');
		  		  });
		  	  }
		  }else{
			  $.messager.alert("提示信息","请选择一条记录!");
		  }
	},
	deletes:function(){
		var node = $('#line_data').treegrid('getSelected');
		if(node!=null){
			$.post(basePath+"/lineConfig/delete.json",{'id':node.id},function(res){
				if(res.success){
					$('#line_data').treegrid('reload');
				}else{
					$.messager.alert("提示信息",res.msg);
				}
		  });
		}else{
			$.messager.alert("提示信息","请选择一条记录!");
		}
	},
	refresh:function(){
		$('#line_data').treegrid('reload');
	},
	
	addEquip:function(){
		var node = $('#line_data').treegrid('getSelected');
		if(node!=null){
			$("#lineId").val(node.id);
			var awin = $('#addEquipWin');
			awin.panel({title: "新增设备"}).dialog("open");
		}else{
			 $.messager.alert("提示信息","请选择一条记录!");
		}
	},
	editEquip:function(){
		  var node = $('#equip_data').datagrid('getSelected');
		  if(node!=null){
		  	  var ewin = $('#editEquipWin');
			  $.post(basePath+"/equip/load.json",{'id':node.id},function(res){
			  		ewin.form('load', {
			  			equName : res.data.equName,
			  			equNo : res.data.equNo,
			  			equMoldId:res.data.equMoldId,
			  			ratedPower:res.data.ratedPower,
			  			supplier:res.data.supplier,
			  			life:res.data.life,
						remark : res.data.remark,
						isCollect:res.data.isCollect,
						id:res.data.id,
						lineId:res.data.lineId
					}).dialog('open');
			  });
		  }else{
			  $.messager.alert("提示信息","请选择一条记录!");
		  }
	},
	deleteEquip:function(){
		var node = $('#equip_data').datagrid('getSelected');
		if(node!=null){
			$.ajax({
				url: basePath+"/equip/delete.json",
				type:'post',
				data:{"id":node.id},
				success:function(result,response,status){
					if(result.success){
						$('#equip_data').datagrid('reload');
					}else{
						$.messager.alert("提示信息",result.msg);
					}
				}
			});
		}else{
			$.messager.alert("提示信息","请选择一条记录!");
		}
	}
};
	
$(function(){
	/**
	 * 车间生产线
	 */
	$('#line_data').treegrid({
 		 url: basePath+"/lineConfig/findByTreePage.json",
 		 method:'get',
 		 rownumbers: true,
 		 pagination: true,
 		 pageSize: 5,
 		 pageList: [5,10,20],
	     idField: 'id',
	     treeField: 'name',
	     toolbar : '#tb',
	     striped : true,
		 nowrap : true,
		 rownumbers : true,
		 fitColumns : true,
		 border:false,
		 columns:[[
	          {title:'名称',field:'name',width:100},
	          {title:'编号',field:'linesNo',width:100},
	          {title:'负责人',field:'leader',width:100},
	          {title:'是否为生产线',field:'isLine',width:100},
	          {title:'是否采集数据',field:'isCollect',width:100},
	          {title:'创建时间',field:'createTime',width:100 ,formatter: function(value){return moment(value).format("YYYY-MM-DD");}},//1451635437000
	          {title:'备注',field:'remark',width:300}
		  ]],
		  onLoadSuccess:function(){
			  $('#line_data').treegrid('collapseAll');
		  },
		  onClickRow:function(row){
			  $('#equip_data').datagrid({
				    url: basePath+"/equip/findByLine.json",
				    queryParams:{lineId:row.id}
			   });
		  }
	});
	
	//添加车间
	$('#addWsWin').dialog({
		width:400,
		height:350,
		iconCls:'icon-add',
		closed: true,
		modal: true,
		inline:true,
		buttons:[{
			text:'提交',
			iconCls:'icon-add-new',
			handler:function(){
				if($('#addWsform').form('validate')){
					$.post(basePath+"/lineConfig/save.json",$("#addWsform").serialize(),function(data){
						if(data.success){
							$('#addWsWin').dialog('close');
							$('#line_data').treegrid('reload');
						}else{
							$.messager.alert({showSpeed:100,title:'消息提示',msg:data.msg,timeout:3000});
						}
					});
				}
			}
		},{
			text:'取消',
			iconCls:'icon-redo',
			handler:function(){
				$('#addWsWin').dialog('close');
			}
		}],
		onClose: function() {
			$('#addWsform').form('reset');
		}
	});
	
	//编辑车间
	$('#editWsWin').dialog({
		width:400,
		height:350,
		iconCls:'icon-edit',
		closed: true,
		modal: true,
		inline:true,
		buttons:[{
			text:'提交',
			iconCls:'icon-add-new',
			handler:function(){
				if($('#editWsform').form('validate')){
					$.post(basePath+"/lineConfig/update.json",$('#editWsform').serialize(),function(data){
						if(data.success){
							$('#editWsWin').dialog('close');
							$('#line_data').treegrid('reload');
						}else{
							$.messager.alert({showSpeed:100,title:'消息提示',msg:data.msg,timeout:3000});
						}
					});
				}
			}
		},{
			text:'取消',
			iconCls:'icon-redo',
			handler:function(){
				$('#editWsWin').dialog('close');
			}
		}],
		onClose: function() {
			$('#editWsform').form('reset');
		}
	});
	
	
	//添加生产线
	$('#addLineWin').dialog({
		width:400,
		height:350,
		iconCls:'icon-add',
		closed: true,
		modal: true,
		inline:true,
		buttons:[{
			text:'提交',
			iconCls:'icon-add-new',
			handler:function(){
				if($('#addLineform').form('validate')){
					$.post(basePath+"/lineConfig/save.json",$("#addLineform").serialize(),function(data){
						if(data.success){
							$('#addLineWin').dialog('close');
							$('#line_data').treegrid('reload');
						}else{
							$.messager.alert({showSpeed:100,title:'消息提示',msg:data.msg,timeout:3000});
						}
					});
				}
			}
		},{
			text:'取消',
			iconCls:'icon-redo',
			handler:function(){
				$('#addLineWin').dialog('close');
			}
		}],
		onClose: function() {
			$('#addLineform').form('reset');
		}
	});

	//编辑生产线
	$('#editLineWin').dialog({
		width:400,
		height:350,
		iconCls:'icon-edit',
		closed: true,
		modal: true,
		inline:true,
		buttons:[{
			text:'提交',
			iconCls:'icon-add-new',
			handler:function(){
				if($('#editLineform').form('validate')){
					$.post(basePath+"/lineConfig/update.json",$('#editLineform').serialize(),function(data){
						if(data.success){
							$('#editLineWin').dialog('close');
							$('#line_data').treegrid('reload');
						}else{
							$.messager.alert({showSpeed:100,title:'消息提示',msg:data.msg,timeout:3000});
						}
					});
				}
			}
		},{
			text:'取消',
			iconCls:'icon-redo',
			handler:function(){
				$('#editLineWin').dialog('close');
			}
		}],
		onClose: function() {
			$('#editLineform').form('reset');
		}
	});
	
	/**
	 * 设备
	 */
	$('#equip_data').datagrid({
	    method: 'get',
	    striped : true,
		nowrap : true,
		rownumbers : true,
		pagination: true,
		fitColumns : true,
		singleSelect:true,
		border:false,
		toolbar:'#tb_sb',
		scrollbarSize:1,
		pageSize: 2,
	    columns:[[
	    	{field:'equNo',align:'center','title':'设备编号',width:150},
			{field:'equName',align:'center','title':'设备名称',width:200},
			{field:'type',align:'center','title':'设备类型',width:200},
			{field:'ratedPower',align:'center','title':'额定功率',width:100},
			{field:'supplier',align:'center','title':'生产厂家',width:150},
			{field:'life',align:'center','title':'使用寿命',width:150},
			{field:'isCollect',align:'center',title:'是否采集数据',width:100},
			{field:'photo',align:'center','title':'编辑图片',width:150,formatter:rowformater,onClickRow:showEquip}
	    ]],
	    pagination : true,
  		pageList : [5,10,20],
  		pageNumber : 1,
		pageSize : 10,
  		pagePosition : 'bottom'
	});
	
	function rowformater(value, row, index) {  
//        return '<a href="javascript:;" onclick="showEquip(' + index + ')">编辑</a>';  
        return '<a href="javascript:;" onclick="showEquip('+index+')">编辑</a>'; 
    }  
	
	//设备类型
	$('#cc').combobox({    
		required:true,
		editable:false,
	    url:basePath+"/equMold/findAll.json",    
	    valueField:'id',
	    textField:'text',
	    onChange:function(data){
	    	$.post(basePath+"/equMold/load.json",{'id':data},function(res){
	    		$("#addratedPower").val(res.data.ratedPower);
	    		$("#addsupplier").val(res.data.supplier);
	    		$("#addlife").val(res.data.life);
		  });
	    }
	});
	$('#cc1').combobox({    
		required:true,
		editable:false,
	    url:basePath+"/equMold/findAll.json",    
	    valueField:'id',
	    textField:'text',
	    onChange:function(data){
	    	$.post(basePath+"/equMold/load.json",{'id':data},function(res){
	    		$("#editratedPower").val(res.data.ratedPower);
	    		$("#editsupplier").val(res.data.supplier);
	    		$("#editlife").val(res.data.life);
		  });
	    }
	});
	
	//添加设备
	$('#addEquipWin').dialog({
		width:350,
		height:500,
		iconCls:'icon-add',
		closed: true,
		modal: true,
		inline:true,
		buttons:[{
			text:'提交',
			iconCls:'icon-add-new',
			handler:function(){
				if($('#addEquipform').form('validate')){
					$('#addEquipform').form('submit',{
						url:basePath+"/equip/save.json",
					    success: function(data){
					        var data = eval('(' + data + ')');  // change the JSON string to javascript object
					        if (!data.success){
					            $.messager.alert("提示信息",data.msg);
					        }else{
					        	$('#addEquipWin').dialog('close');
					        	$('#equip_data').datagrid('reload');
					        }
					    }
					});
				}
			}
		},{
			text:'取消',
			iconCls:'icon-redo',
			handler:function(){
				$('#addEquipWin').dialog('close');
			}
		}],
		onClose: function() {
			$('#addEquipform').form('reset');
		}
	});
	
	//编辑设备
	$('#editEquipWin').dialog({
		title:'编辑',
		width:350,
		height:500,
		iconCls:'icon-edit',
		closed: true,
		modal: true,
		inline:true,
		buttons:[{
			text:'提交',
			iconCls:'icon-add-new',
			handler:function(){
				if($('#editEquipform').form('validate')){
					$('#editEquipform').form('submit',{
						url:basePath+"/equip/update.json",
					    success: function(data){
					        var data = eval('(' + data + ')');  // change the JSON string to javascript object
					        if (!data.success){
					            $.messager.alert("提示信息",data.msg);
					        }
					        $('#editEquipWin').dialog('close');
							$('#equip_data').datagrid('reload');
					    }
					});
				}
			}
		},{
			text:'取消',
			iconCls:'icon-redo',
			handler:function(){
				$('#editEquipWin').dialog('close');
			}
		}],
		onClose: function() {
			$('#editEquipform').form('reset');
		}
	});
	
	
	//编辑设备
	$('#editPhotoWin').dialog({
		title:'编辑',
		width:350,
		height:250,
		iconCls:'icon-edit',
		closed: true,
		modal: true,
		inline:true,
		buttons:[{
			text:'提交',
			iconCls:'icon-add-new',
			handler:function(){
				if($('#editPhotoform').form('validate')){
					$('#editPhotoform').form('submit',{
						url:basePath+"/equip/updatePhoto.json",
					    success: function(data){
					        var data = eval('(' + data + ')');  // change the JSON string to javascript object
					        if (!data.success){
					            $.messager.alert("提示信息",data.msg);
					        }
					        $('#editPhotoWin').dialog('close');
							$('#equip_data').datagrid('reload');
					    }
					});
				}
			}
		},{
			text:'取消',
			iconCls:'icon-redo',
			handler:function(){
				$('#editPhotoWin').dialog('close');
			}
		}],
		onClose: function() {
			$('#editPhotoform').form('reset');
		}
	});
});
